//
//  ST_PinterestHomeCell.swift
//  btvideo
//
//  Created by mac on 2019/12/23.
//  Copyright © 2019 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit
import NIMSDK
import SwiftyUserDefaults


class ST_PinterestHomeCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor = Color.white
        
        //        self.contentView.backgroundColor = Color.random
        
        contentView.addSubview(cover_image_view)
        contentView.addSubview(info_label)
        contentView.addSubview(icon_image_view)
        contentView.addSubview(content_label)
        
        
        cover_image_view.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(50)
        }
        
        
        
        info_label.snp.makeConstraints { (make) in
            make.top.equalTo(cover_image_view.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-15)
        }
        
        icon_image_view.snp.makeConstraints { (make) in
            make.centerY.equalTo(info_label)
            make.left.equalToSuperview().offset(5)
            make.width.equalTo(4)
            make.height.equalTo(6)
        }
        
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(info_label.snp.bottom).offset(5)
            make.left.equalTo(info_label)
            make.right.equalToSuperview()
        }
        
        
        let moreButtonW = 13.cgFloat
        
        contentView.addSubview(more_button)
        more_button.snp.makeConstraints { (make) in
            make.centerY.equalTo(info_label)
            make.right.equalToSuperview().offset(-5+(44-moreButtonW)/2.0)
            make.width.height.equalTo(44)
        }
        
        //        self.contentView.addSubview(operateBtn)
        //        operateBtn.snp.makeConstraints { (make) in
        //            make.edges.equalToSuperview()
        //        }
        
        contentView.addSubview(rightSignLabel)
        rightSignLabel.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 40, height: 22))
            make.left.equalToSuperview().offset(8)
            make.top.equalToSuperview().offset(8)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //    fileprivate lazy var pauseSignImageView:UIImageView = {
    //        let control = UIImageView.init(image: ST_ImageUtils.imageName("pause"))
    //        control.isHidden = true
    //        return control
    //    }()
    
    
    fileprivate(set) lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        control.cornerRadius = 20
        return control
    }()
    
    
    lazy var rightSignLabel:UILabel = {
        let control = UILabel()
        control.text = "真人"
        control.isHidden = true
        control.cornerRadius = 11
        control.textAlignment = .center
        control.textColor = UIColor.init(hex: 0x333333)
        control.backgroundColor = Color.white
        control.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var icon_image_view:ST_TriangleView = {
        let control = ST_TriangleView()
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        control.isHidden = true
        return control
    }()
    
    fileprivate  lazy var info_label:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var more_button:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("首页_icon_更多操作")!)
        control.add_touch_up(insideTarget: self, action: #selector(more_btn_click_handle))
        //        control.backgroundColor = Color.random
        return control
    }()
    
    var ignore_somebody_closure:((_ userId:String,_ noticeText:String)->())?
    
    //    MARK: - Bind Model
    var model = ST_PinterestHomeCellModel(){
        didSet{
            //            pauseSignImageView.isHidden = model.type == .image
            
            info_label.text = model.age.string + "岁 " + model.address
            
            if let attr = model.content_attr{
                content_label.attributedText = attr
                content_label.isHidden = false
            }else{
                content_label.isHidden = true
            }
            
            if let first = model.resourceList.first{
                
                
                //                cover_image_view.pin_setImage(from: first.imageUrl.compress_image().url, completed: nil)
                
                cover_image_view.pin_setImage(from: first.imageUrl.compress_image().url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
            }
            
            cover_image_view.snp.updateConstraints { (make) in
                make.height.equalTo(model.firstImageSize.height)
            }
            
            self.rightSignLabel.isHidden = !model.faceIdentified
        }
    }
}

//MARK: - public method
extension ST_PinterestHomeCell{
    
    
    func picutreframe_in_content_view()->CGRect{
        
        return self.cover_image_view.frame
        
    }
    
    @objc func more_btn_click_handle(){
        
        let vc = ST_BTSheetActionController.init(title: nil, message: nil)
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        
        let str1 = NSMutableAttributedString.init(string: "不感兴趣")
        
        str1.addAttributes(attr, range: NSRange(location: 0, length: str1.length))
        
        vc.addAction(ST_BTAlertAction.init(title: str1, handler: { (action) in
            
            
            self.ignorePerson(text:"操作成功，你将不会再看到此用户")
        }))
        
        if ST_BTUserInfoManager.shared.current_account_info?.userinfo?.sex == .male || Defaults[.isPanda] {
            
            let str2 = NSMutableAttributedString.init(string: "拉黑")
            
            str2.addAttributes(attr, range: NSRange(location: 0, length: str2.length))
            
            vc.addAction(ST_BTAlertAction.init(title: str2, handler: { (action) in
                
                self.ban_current_user_handle()
                
            }))
        }
        
        let str3 = NSMutableAttributedString.init(string: "举报")
        
        str3.addAttributes(attr, range: NSRange(location: 0, length: str3.length))
        
        vc.addAction(ST_BTAlertAction.init(title: str3, handler: { (action) in
            
            let vc = ST_ReportPersonController()
            vc.userId = self.model.accid
            let nav = UINavigationController.create_hiding_bar_navigation_controller(rootViewController: vc)
            GlobalPresent(toController: nav)
            
        }))
        
        vc.show()
        
    }
    
    
    func ignorePerson(text:String){
        
        self.ignore_somebody_closure?(model.accid,text)
        
    }
    
    
    @objc func ban_current_user_handle(){
        
        
        BAN_SOMEBODY_HANDLE(userId: model.accid, close_view: false, hud_controller: self.parentViewController)
        
    }
    
    
}






//MARK: - Guidance Cell
class PinterestGuidanceHomeCell: UICollectionViewCell {
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.backgroundColor = Color.white
        
        contentView.addSubview(cover_image_view)
        
        cover_image_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
    
    //    MARK: Properties
    fileprivate lazy var cover_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        //        control.cornerRadius = 20
        return control
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    enum PinterestGuidanceType:Int{
        case normal,active,join
    }
    
    fileprivate(set) var guidanceType = PinterestGuidanceType.normal
    
    fileprivate var curr:ST_PinterestHomeCellModel?
    
    func bind_model(_ m:ST_PinterestHomeCellModel){
        
        curr = m
        //        功能 #3170 回到之前逻辑
        cover_image_view.image = UIImage.init(named: m.guidanceImageName)
        
    }
    
    
    func refresh_guidance_status(){
        
        guard let m = curr else { return }
        
        bind_model(m)
        
    }
}
